package com.iiiss.image.controllers;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.multipart.MultipartFile;

import com.iiiss.image.controllers.json.JsonError;
import com.iiiss.image.controllers.json.JsonFile;
import com.iiiss.image.utilities.RenameStrategy;

/*
 * TemplateUpload - com.iiiss.image.controllers - UploadController.java
 * 
 * Copyright (C) 2011  Mingye Cheng <Cheng, Mingye at GMAIL>
 * 
 * UploadController.java is part of <iiiStudio Template Framework> project.
 * This project is hosted at <http://iiiss-template-ssh.googlecode.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Revision history:
 * [2011-7-16] Mingye Cheng - Initial version established.
 */
@Controller
public class UploadController implements ServletContextAware {
	public static final String FILE_STORE_DIRECTORY = "FILE_STORE_DIRECTORY";
	public static final String PROTOCAL_PREFIX = "http://";

	public static final String CALLBACK_PAGE = "callback";
	public static final String JSON_PAGE = "result";
	public static final String JSON_VARIABLE = "json";
	public static final String CALLBACK_VARIABLE = "callback";

	public static final String NO_FILE_MESSAGE = "no file uploaded.";

	@Autowired
	private RenameStrategy renameStrategy;

	private ServletContext servletContext;

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}

	@RequestMapping(value = "/fileupload/single", method = RequestMethod.POST)
	public String single(@RequestHeader String host,
			@RequestParam MultipartFile file,
			@RequestParam(required = false) String callback, Model model)
			throws IllegalStateException, IOException {
		// prepare new filename
		String originalFilename = file.getOriginalFilename();
		String newFilename = renameStrategy.rename(originalFilename);
		if (newFilename.isEmpty()) {
			model.addAttribute(JSON_VARIABLE, new JsonError(NO_FILE_MESSAGE));
			return JSON_PAGE;
		}

		// save the file to server file system
		String fileStoreDirectory = servletContext
				.getInitParameter(FILE_STORE_DIRECTORY);
		String servletContextRealPath = servletContext
				.getRealPath(fileStoreDirectory + newFilename);
		File fileSystemFile = new File(servletContextRealPath);
		file.transferTo(fileSystemFile);

		// prepare absolute URL
		String contextPath = servletContext.getContextPath();
		String absoluteURL = PROTOCAL_PREFIX + host + contextPath
				+ fileStoreDirectory + newFilename;

		// prepare response json
		if (callback == null) {
			model.addAttribute(JSON_VARIABLE, new JsonFile(absoluteURL,
					originalFilename));
			return JSON_PAGE;
		} else {
			model.addAttribute(JSON_VARIABLE, new JsonFile(absoluteURL,
					originalFilename));
			model.addAttribute(CALLBACK_VARIABLE, callback);
			return CALLBACK_PAGE;
		}
	}

	@RequestMapping(value = "/fileupload/multiple", method = RequestMethod.POST)
	public String multiple(Model model) {
		model.addAttribute(JSON_VARIABLE, new JsonError("not implemented."));
		return JSON_PAGE;
	}
}
